version 15
set more off  
clear
capture log close
macro drop _all 

*---------------------------------------------------------------------------*       				
* PROJECT: COMPLAINTS HR	  	    		    							*
* DO FILE: FIGURE 5															*
* AUTHORS: FLORES-MACIAS AND  ZARKIN                    					*
* JANUARY 2023              				         						*
*---------------------------------------------------------------------------*       				
     				

*--------------------------------------------------------------------*
* STEP 1: GLOBALS, ADD WORKING DIRECTORY HERE 				 		 *
*--------------------------------------------------------------------*
*---------------------------------------------------------------------------*
gl final "/Users/jzarkin/Library/CloudStorage/GoogleDrive-jessnotni@gmail.com/My Drive/5_COAUTHOREDPROJECTS/2019_HR/00_REPLICATIONFILES/0_DATA"
gl fig "/Users/jzarkin/Library/CloudStorage/GoogleDrive-jessnotni@gmail.com/My Drive/5_COAUTHOREDPROJECTS/2019_HR/00_REPLICATIONFILES/2_FIGURES"
cd "/Users/jzarkin/Library/CloudStorage/GoogleDrive-jessnotni@gmail.com/My Drive/5_COAUTHOREDPROJECTS/2019_HR/00_REPLICATIONFILES/2_FIGURES"
*---------------------------------------------------------------------------*

*--------------------------------------------------------------------*
* STEP 2: CREATE LOG FILE 				 		 					 *
*--------------------------------------------------------------------*

log using "$fig/DD.log", replace


*--------------------------------------------------------------------*
* STEP 3: DIF-DIF 				 		 					         *
*--------------------------------------------------------------------*

use "$final/ForMatching.dta", clear	

destring inegi, gen(muncode)
xtset muncode year


* TABLE 4A, SUPPL APPENDIX

	areg subsetSECFORCESpc operativo i.year, a(inegi)
	areg subsetSECFORCESpc operativo i.year , a(inegi) robust
	areg subsetSECFORCESpc operativo i.year , a(inegi) vce(cluster inegi)
	
* TABLE 5A, SUPPL APPENDIX

gen samegovpres_before=samegovpres[_n-1] if inegi==inegi[_n-1]
gen thom_before=thom[_n-1] if inegi==inegi[_n-1]
gen quejasCNDHpc_before=quejasCNDHpc[_n-1] if inegi==inegi[_n-1]
rename changeparty partychange

	areg subsetSECFORCESpc operativo i.year , a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc operativo i.year  [w=pob], a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc operativo i.year thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc operativo i.year thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob [w=pob], a(inegi) vce(cluster inegi)

	
* TABLE 6A, SUPPL APPENDIX

	areg subsetSECFORCESpc operativo i.year, a(inegi)
	areg subsetSECFORCESpc operativo i.year i.muncode#c.time, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc operativo i.year i.muncode#c.time [w=pob], a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc operativo i.year i.muncode#c.time thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc 	quejasCNDHpc_before lnpob, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc operativo i.year i.muncode#c.time thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob [w=pob], a(inegi) vce(cluster inegi)
	
	
* 4. TABLE 9A, SUPPL APPENDIX


	areg subsetSECFORCESpc ch* i.year, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year  [w=pob], a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year i.muncode#c.time, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob [w=pob], a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year i.muncode#c.time thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob, a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year i.muncode#c.time [w=pob], a(inegi) vce(cluster inegi)
	areg subsetSECFORCESpc ch* i.year i.muncode#c.time thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob [w=pob], a(inegi) vce(cluster inegi)
	
			
* FIG 19A SUPPLEMENTAL APPENDIX

use "$final/ForMatching.dta", clear	
	
	rename changeparty partychange
	
	gen samegovpres_before=samegovpres[_n-1] if inegi==inegi[_n-1]
	gen thom_before=thom[_n-1] if inegi==inegi[_n-1]
	gen quejasCNDHpc_before=quejasCNDHpc[_n-1] if inegi==inegi[_n-1]

	destring inegi, gen(muncode)
	xtset muncode year
	
	areg subsetSECFORCESpc ch* i.year thom_before turfwar_threeafbefore samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob, a(inegi) vce(cluster inegi)
	regsave using "$fig/OvertimeWo.dta", tstat pval ci replace
	
	use "$fig/OvertimeWo.dta", clear
	
		keep if _n<11
		
		gen count=_n
		set obs 12
		replace var="ch0" in 11
		replace var="chneg" in 12
		replace count=0 in 11
		replace count=-1 in 12
		
		foreach x of varlist coef  ci_lower ci_upper {
		recode `x' .=0
		}
		
		sort count
		
		twoway (rarea ci_upper ci_lower count, fcolor(gs10) lcolor(gs10) lpattern(solid)) ///
			(line coef count, lcolor(black) lpattern(solid)) (scatter coef count, color(black)), ///
			xlab(-1(1)10) ylab(-3(1)4)  ///
			xtitle("Accumulated years of military presence for public security", size(small)) ///
			ytitle("Reg coefficient: effect on serious HR complaints rate", size(small)) ///
			legend(off) yline(0, lcolor(gs4) lpattern(solid) lwidth(vthin))
				
		graph export "$fig/SA_FIG19A.png", replace





	
*--------------------------------------------------------------------*
* STEP 4: BACON DECOMPOSITION 	 		 					         *
*--------------------------------------------------------------------*

use "$final/ForMatching.dta", clear	

* CANNOT RUN IF HAVE TREATMENT REVERSAL; KEEP PERMANMENT OPERATIONS

	keep inegi year operativo
	rename operativo op
	reshape wide op, i(inegi) j(year)
	
	gen permanent = 1 if op2007==1  & op2008==1 &  ///
				op2009==1 & op2010==1 & op2011==1 & op2012==1 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1
	replace permanent = 1 if op2007==0  & op2008==1 &  ///
				op2009==1 & op2010==1 & op2011==1 & op2012==1 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1	
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==1 & op2010==1 & op2011==1 & op2012==1 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==1 & op2011==1 & op2012==1 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1				
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==1 & op2012==1 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1					
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==0 & op2012==1 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1					
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==0 & op2012==0 & ///
				op2013==1 & op2014==1 & op2015==1 & op2016==1					
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==0 & op2012==0 & ///
				op2013==0 & op2014==1 & op2015==1 & op2016==1									
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==0 & op2012==0 & ///
				op2013==0 & op2014==0 & op2015==1 & op2016==1					
	replace permanent = 1 if op2007==0  & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==0 & op2012==0 & ///
				op2013==0 & op2014==0 & op2015==0 & op2016==1	
	recode permanent .= 0 if op2007==0 & op2008==0 &  ///
				op2009==0 & op2010==0 & op2011==0 & op2012==0 & ///
				op2013==0 & op2014==0 & op2015==0 & op2016==0 
	recode permanent .=-1
	
	reshape long op, i(inegi permanent) j(year)
			
		keep inegi year permanent
		merge 1:1 inegi year using "$final/ForMatching.dta"
		drop _merge
		drop if permanent==-1
		
	destring inegi, gen(muncode)
	xtset muncode year
	
	gen samegovpres_before=samegovpres[_n-1] if inegi==inegi[_n-1]
	gen thom_before=thom[_n-1] if inegi==inegi[_n-1]
	gen quejasCNDHpc_before=quejasCNDHpc[_n-1] if inegi==inegi[_n-1]

	xtreg subsetSECFORCESpc operativo thom_before turfwar_threeafbefore ///
		samegovpres_before pobh15a44pc quejasCNDHpc_before lnpob i.year, fe robust
	
	bacondecomp subsetSECFORCESpc operativo thom_before turfwar_threeafbefore samegovpres_before ///
		pobh15a44pc quejasCNDHpc_before lnpob, stub(Bacon2_) robust ///
		msymbols(O S N) msize(small small small) ///
		mcolor(turquoise%50 orangebrown%50 sky%50) ///
		gropt(legend(position(6) r(1)) ///
		yline(0, lcolor(gs10) lpattern(dash) lwidth(medium))) ///
		ddline(lcolor(black) lpattern(solid))
		
		*graph export "$fig/FIG17A.png"
		
			*USED GRAPH EDITOR TO CHANGE THICKNESS OF LINE AND ADD TEXT
		
		drop Bacon*
	
	
	bacondecomp subsetSECFORCESpc operativo thom_before turfwar_threeafbefore samegovpres_before ///
		pobh15a44pc quejasCNDHpc_before lnpob ///
		[w=pob2010], stub(Bacon3_) robust ///
		msymbols(O S N) msize(small small small) ///
		mcolor(turquoise%50 orangebrown%50 sky%50) ///
		gropt(legend(position(6) r(1)) ///
		yline(0, lcolor(gs10) lpattern(dash) lwidth(medium))) ///
		ddline(lcolor(black) lpattern(solid))
		
		*graph export "$fig/FIG18A.png"
		
			*USED GRAPH EDITOR TO CHANGE THICKNESS OF LINE AND ADD TEXT
	
*--------------------------------------------------------------------*
* STEP 5: CLOSE LOG				 		 					         *
*--------------------------------------------------------------------*

log close				
